from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

# 1.获取数据
iris = load_iris()
x = iris.data
y = iris.target,
# 2.数据基本处理
trainX, testX, trainY, testY = train_test_split(x, y, test_size=0.2, random_state=22)


# 3.特征工程 - 特征预处理
transfer = StandardScaler()
trainX = transfer.fit_transform(trainX)
testX = transfer.transform(testX)

# 4.机器学习 - KNN
# 4.1.实例化估算器
# estimator = KNeighborsClassifier(n_neighbors=5)
estimator = KNeighborsClassifier()

# 4.2.模型调优，交叉验证的KNeighborsClassifier没有参数，CV是下面传超参数+++++++
param_grid = {"n_neighbors": [1, 3, 5, 7, 9]}  # 当数据量比较多的时候，超参数一定不要传太多！！！
estimator = GridSearchCV(estimator, param_grid=param_grid, cv=5)

# 4.3.模型训练
estimator.fit(trainX, trainY)

# 5.模型评估
yPre = estimator.predict(testX)
# 5.1.预测值结果输出
print("预测值是", yPre)
print("预测值和真实值的对比", yPre == testY)

# 5.2.准确率计算
score = estimator.score(testX, testY)
print("准确率=", score)

print("交叉验证中最好模型=", estimator.best_estimator_)
print("交叉验证中最好结果=", estimator.best_score_)
print("交叉验证中得到的所有模型结果=", estimator.cv_results_)
